package view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import dao.MenuDao;
import modle.Country;
import modle.Menu;
import util.DbUtil;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.Image;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.*;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import java.awt.Toolkit;

public class Order extends JFrame {

	private JPanel contentPane;
	private JTextField Num;
	private JLabel label;
	private HashMap<String, String> map = new HashMap<String, String>();
	private String ss = null;
	private JLabel orderLabel;
	private JLabel quantityLabel;
	private JButton btnAdd;
	private JButton btnBack = new JButton("Back");
	private JLabel Money;
	private JButton btnCheck;
	private JComboBox comboBox;
	private List<Country> listCountries;
	private Locale locale;
	ResourceBundle rd;
	private JTabbedPane tabbedPane;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane_1;
	private JScrollPane scrollPane_2;
	private JScrollPane scrollPane_3;
	private JTextArea textArea_4;
	private JTextArea textArea_1;
	private JTextArea textArea_2;
	private JTextArea textArea_3;
	private JLabel backgroundlabel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Order frame = new Order(1);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Order(int id) {
		setTitle("Welcome to Food Corner");
		setIconImage(Toolkit.getDefaultToolkit().getImage(Order.class.getResource("/img/iconImage.jpg")));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 699, 490);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		orderLabel = new JLabel("Order Food from Category");
		orderLabel.setForeground(new Color(210, 105, 30));
		orderLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 23));
		orderLabel.setBounds(216, 45, 338, 39);
		contentPane.add(orderLabel);
		
		Money = new JLabel("Money");
		Money.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		Money.setBounds(427, 324, 86, 27);
		contentPane.add(Money);
		
		label = new JLabel();
		label.setIcon(null);
		label.setBounds(359, 158, 154, 153);
		contentPane.add(label);
		
		btnCheck = new JButton("Check");
		btnCheck.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 16));
		btnCheck.setBounds(546, 376, 102, 34);
		contentPane.add(btnCheck);
		btnCheck.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("正在进入菜单......");
				if(map.size()==0)   JOptionPane.showMessageDialog(null, "对不起，您还没有点餐");
				else
				{
					new Bill(id, map).setVisible(true);
					dispose();
				}
			}
		});
		
		btnAdd = new JButton("Add");
		btnAdd.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 17));
		btnAdd.setBounds(562, 263, 86, 34);
		contentPane.add(btnAdd);
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(ss!=null)
					map.put(ss, Num.getText());
			}
		});
		
		quantityLabel = new JLabel("Quantity:");
		quantityLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 19));
		quantityLabel.setBounds(548, 146, 119, 27);
		contentPane.add(quantityLabel);
		
		Num = new JTextField();
		Num.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 19));
		Num.setBounds(562, 207, 86, 24);
		contentPane.add(Num);
		Num.setColumns(10);
		
		comboBox = new JComboBox<>();						//组合按钮或可编辑字段和下拉列表的组件
		comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		comboBox.addItemListener(new ItemListener() {		//用于接收项目事件的侦听器接口。
			public void itemStateChanged(ItemEvent e) {
				int index = comboBox.getSelectedIndex();	//返回列表中与给定项目匹配的第一个项目。 
					String arr[] = listCountries.get(index).getId().split("-");					//  ???（应该是遇见 - 就分割）
					locale = new Locale(arr[0], arr[1]);//	Locale对象代表具体的地理，政治或文化地区。（第一个是语言，第二个是国家）
					rd = ResourceBundle.getBundle("resources.content", locale);		//使用指定的基本名称，区域设置和类加载器获取资源包（这里好像没用类加载器）。 
					
					//rd到底有什么用
					
					try {
						changeLanguage();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
			}
		});
		comboBox.setBounds(569, 13, 98, 32);
		contentPane.add(comboBox);
		
//		textarea = new JTextArea();
//		textarea.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
//		textarea.addCaretListener(new CaretListener() {		//添加插入符监听器以通知插入符的任何更改。 
//			public void caretUpdate(CaretEvent e) {		//caretUpdate方法，当插入位置更新时调用。 
//				ss = textarea.getSelectedText();		//getSelectedText方法：返回所选文本的部分，若无则返回null
//				if(ss!=null)
//				{
//					Menu nowmenu = findMenu(ss);
//					if(nowmenu!=null)
//					{
//						label.setIcon(ResizeImage(nowmenu.getPicture()));
////						ResizeImage("C:/Users/leepanker/git/repository/Exercise/src/img/炒青菜.jpg")
//						Money.setText(nowmenu.getPrice());
//					}
//				}
//			}
//		});
//		textarea.setEditable(false);
//		ScrollPane scrollPane = new ScrollPane();
//		scrollPane.setBounds(30, 132, 189, 207);
//		scrollPane.add(textarea);
//		contentPane.add(scrollPane);
//		Fill();
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(14, 128, 331, 233);
		contentPane.add(tabbedPane);
		
		scrollPane = new JScrollPane();
		tabbedPane.addTab("主食", null, scrollPane, null);
		
		textArea_1 = new JTextArea();
		textArea_1.setFont(new Font("Monospaced", Font.PLAIN, 17));
		scrollPane.setViewportView(textArea_1);
		textArea_1.addCaretListener(new CaretListener() {		//添加插入符监听器以通知插入符的任何更改。 
			public void caretUpdate(CaretEvent e) {		//caretUpdate方法，当插入位置更新时调用。 
				ss = textArea_1.getSelectedText();		//getSelectedText方法：返回所选文本的部分，若无则返回null
				if(ss!=null)
				{
					Menu nowmenu = findMenu(ss);
					if(nowmenu!=null)
					{
						label.setIcon(ResizeImage(nowmenu.getPicture(), label));
	//					ResizeImage("C:/Users/leepanker/git/repository/Exercise/src/img/炒青菜.jpg")
						Money.setText(nowmenu.getPrice());
					}
				}
			}
		});
		textArea_1.setEditable(false);
		
		scrollPane_1 = new JScrollPane();
		tabbedPane.addTab("主菜", null, scrollPane_1, null);
		
		textArea_2 = new JTextArea();
		textArea_2.setFont(new Font("Monospaced", Font.PLAIN, 17));
		scrollPane_1.setViewportView(textArea_2);
		textArea_2.addCaretListener(new CaretListener() {		//添加插入符监听器以通知插入符的任何更改。 
		public void caretUpdate(CaretEvent e) {		//caretUpdate方法，当插入位置更新时调用。 
			ss = textArea_2.getSelectedText();		//getSelectedText方法：返回所选文本的部分，若无则返回null
			if(ss!=null)
			{
				Menu nowmenu = findMenu(ss);
				if(nowmenu!=null)
				{
					label.setIcon(ResizeImage(nowmenu.getPicture(), label));
//					ResizeImage("C:/Users/leepanker/git/repository/Exercise/src/img/炒青菜.jpg")
					Money.setText(nowmenu.getPrice());
				}
			}
		}
	});
		textArea_2.setEditable(false);
		
		scrollPane_2 = new JScrollPane();
		tabbedPane.addTab("饮品", null, scrollPane_2, null);
		
		textArea_3 = new JTextArea();
		textArea_3.setFont(new Font("Monospaced", Font.PLAIN, 17));
		scrollPane_2.setViewportView(textArea_3);
		textArea_3.addCaretListener(new CaretListener() {		//添加插入符监听器以通知插入符的任何更改。 
		public void caretUpdate(CaretEvent e) {		//caretUpdate方法，当插入位置更新时调用。 
			ss = textArea_3.getSelectedText();		//getSelectedText方法：返回所选文本的部分，若无则返回null
			if(ss!=null)
			{
				Menu nowmenu = findMenu(ss);
				if(nowmenu!=null)
				{
					label.setIcon(ResizeImage(nowmenu.getPicture(), label));
					Money.setText(nowmenu.getPrice());
				}
			}
		}
	});
		textArea_3.setEditable(false);
		
		scrollPane_3 = new JScrollPane();
		tabbedPane.addTab("甜品", null, scrollPane_3, null);
		
		textArea_4 = new JTextArea();
		textArea_4.setFont(new Font("Monospaced", Font.PLAIN, 17));
		scrollPane_3.setViewportView(textArea_4);
		textArea_4.addCaretListener(new CaretListener() {		//添加插入符监听器以通知插入符的任何更改。 
		public void caretUpdate(CaretEvent e) {		//caretUpdate方法，当插入位置更新时调用。 
			ss = textArea_4.getSelectedText();		//getSelectedText方法：返回所选文本的部分，若无则返回null
			if(ss!=null)
			{
				Menu nowmenu = findMenu(ss);
				if(nowmenu!=null)
				{
					label.setIcon(ResizeImage(nowmenu.getPicture(), label));
					Money.setText(nowmenu.getPrice());
				}
			}
		}
	});
		textArea_4.setEditable(false);
		
		fillCountries();
		Fill();
		
//		JLabel lblNewLabel = new JLabel("");
//		lblNewLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
//		lblNewLabel.setBounds(31, 23, 154, 32);
//		contentPane.add(lblNewLabel);
//		lblNewLabel.setText("Hi " + s + " !");
		
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		btnBack.setBounds(14, 397, 91, 34);
		contentPane.add(btnBack);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Customer_Menu(id).setVisible(true);
				dispose();
			}
		});
	}
	
	private ImageIcon ResizeImage(String imgpath, JLabel label) {
		// TODO Auto-generated method stub
		ImageIcon img1 = new ImageIcon(imgpath);
		Image img2 = img1.getImage();
		Image img3 = img2.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
		ImageIcon img4 = new ImageIcon(img3);
		return img4;
	}

	protected Menu findMenu(String ss) {
		// TODO Auto-generated method stub
		
		try {
			Menu menu = MenuDao.MenuSearch3(DbUtil.getCon(), ss);
			return menu;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private void Fill() {
		// TODO Auto-generated method stub
		for(int i=1; i<100; ++i)
		{
			try {
				Menu menu = MenuDao.MenuSearch2(DbUtil.getCon(), i);
				if(menu==null)   break;
				String s = menu.getId();
				if(s.charAt(1)=='f')
					textArea_1.append(menu.getName() + '\n');
				else if(s.charAt(1)=='u')
					textArea_2.append(menu.getName() + '\n');
				else if(s.charAt(1)=='r')
					textArea_3.append(menu.getName() + '\n');
				else if(s.charAt(1)=='e')
					textArea_4.append(menu.getName() + '\n');
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private void fillCountries() {
		// TODO Auto-generated method stub
		listCountries = new ArrayList<Country>();
		listCountries.add(new Country("en-US", "English"));
		listCountries.add(new Country("zh-CN", "中文"));
		
		for(Country c : listCountries)
			comboBox.addItem(c.getName());
	}

	protected void changeLanguage() {
		// TODO Auto-generated method stub
		
		btnBack.setText(rd.getString("btnBack"));
		orderLabel.setText(rd.getString("orderLabel"));
		quantityLabel.setText(rd.getString("quantityLabel"));
		btnAdd.setText(rd.getString("btnAdd"));
		Money.setText(rd.getString("Money"));
		btnCheck.setText(rd.getString("btnCheck"));
	}
}
